#coding=utf-8
#!/usr/bin/python3
# @author:ZhaiGuiDe
# @Time:2021/6/3/ 9:51
import time

import requests
import threading
import gevent

from load_video_110_53.db_connect import MysqlClient
from load_video_110_53.load_video_110_53_81 import ADMIN_PORT, ADMIN_IP, VIDEO_CHECK_STATUS, ProcessVideo


class CheckVideoStatus:
    # 未审核的视频入库
    def __init__(self):
        self.dbc = MysqlClient()
        self.pv = ProcessVideo()

    def get_status_id(self):
        #查询未入库的视频且 是由admin2000上传的视频的id
        select_checked_1_sql = """
           SELECT video_id FROM `tb_video_origin` where checked=1 AND create_by="admin2000";
       """
        result = self.dbc.dataSelect(select_checked_1_sql)
        # print(result)
        return result

    def check_video(self,video_id):
        # 拿到视频id进行修改视频状态
        url = "http://"+ADMIN_IP+":"+ADMIN_PORT +  VIDEO_CHECK_STATUS
        headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Content-Length": "22",
            "Content-Type": "application/x-www-form-urlencoded",
            "Host": ADMIN_IP + ":" + ADMIN_PORT,
            "Origin": "http://" + ADMIN_IP + ":" + ADMIN_PORT,
            "Pragma": "no-cache",
            "Referer": "http://" + ADMIN_IP + ":" + ADMIN_PORT + "/admin/",
            "token": self.pv.get_token(),
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4449.6 Safari/537.36",
        }
        data = {"ids": video_id}
        res = requests.put(url=url,headers=headers,data=data)
        print(res.json())

    def get_id_list():
        #拿到id列表
        checkvi = CheckVideoStatus()
        video_id_list = []
        for tuple_id in checkvi.get_status_id():
            video_id_list.append(tuple_id[0])
        # print(video_id_list)
        return video_id_list

def main():
    ck = CheckVideoStatus()
    for id in CheckVideoStatus.get_id_list():
        # print(id)
        #s使用协程处理
        gevent.joinall([
            gevent.spawn(ck.check_video,id)
        ])
        # 使用多线程处理
        # t = threading.Thread(target=checkvi.check_video, args=(video_id,))
        # t.start()
        # t.join()
    print("修改状态完毕，全部入库")

if __name__ == '__main__':
        # video_count= CheckVideoStatus.get_id_list()
        # print("*" * 20, len(video_count), "*" * 20)
        main()

